﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    public partial class WenCircleProgress : UserControl
    {
        public WenCircleProgress()
        {
            InitializeComponent();
            base.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
        }
        #region 私有属性
        private int value = 0;
        #endregion

        #region 公有属性
        [Category("Wen")]
        [Description("圆点大小")]
        [DefaultValue(40)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public int PSize { get; set; } = 40;

        [Category("Wen")]
        [Description("当前进度值")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public int Value
        {
            get => this.value;
            set
            {
                this.value = value > 100 ? 100 : value;
                this.Invalidate();
            }
        }
        #endregion

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Width = Height;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            CircleRe(e);
        }

        //绘制圆 分别分布点
        private void CircleRe(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            ControlHelper.SetGDIHigh(g);

            //改变绘制的圆点，便于计算  和数学方向一致

            g.TranslateTransform(0f, this.Height);
            g.ScaleTransform(1f, -1f);


            Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);

            //时针球 大小

            int pr = PSize;

            //Brush b = Brushes.Gray;

            int r = (rec.Width - pr) / 2;

            int rc = rec.Width / 2;

            //计算 0 位置

            Rectangle val0 = new Rectangle(rec.Width / 2 - pr / 2, rec.Height - pr, pr, pr);
            Rectangle val25 = new Rectangle(rec.Width - pr, rec.Height / 2 - pr / 2, pr, pr);
            Rectangle val50 = new Rectangle(rec.Width / 2 - pr / 2, 0, pr, pr);
            Rectangle val75 = new Rectangle(0, rec.Height / 2 - pr / 2, pr, pr);

            //计算45度位置的值

            float l = (float)Math.Sqrt(r * r / 2);

            RectangleF val12_5 = new RectangleF(rc + l - pr / 2, rc + l - pr / 2, pr, pr);
            RectangleF val37_5 = new RectangleF(rc + l - pr / 2, rc - l - pr / 2, pr, pr);
            RectangleF val62_5 = new RectangleF(rc - l - pr / 2, rc - l - pr / 2, pr, pr);
            RectangleF val87_5 = new RectangleF(rc - l - pr / 2, rc + l - pr / 2, pr, pr);

            g.FillEllipse(GetBrush(0), val0);
            g.FillEllipse(GetBrush(25), val25);
            g.FillEllipse(GetBrush(50), val50);
            g.FillEllipse(GetBrush(75), val75);
            g.FillEllipse(GetBrush(12.5), val12_5);
            g.FillEllipse(GetBrush(37.5), val37_5);
            g.FillEllipse(GetBrush(62.5), val62_5);
            g.FillEllipse(GetBrush(87.5), val87_5);
        }

        private Brush GetBrush(double value)
        {
            if (value < Value)
                return Brushes.Gray;
            else
                return Brushes.LightGray;
        }
    }
}